/*
 * VHLeagueStandingsTab.java
 * Created on 30 juin 2009
 *
 * VirtualHockey Internet Platform
 * http://code.google.com/p/virtualhockey/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

package com.hockeo.vhweb.client.ui.tabs;

import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.hockeo.vhweb.client.VHMain;
import com.hockeo.vhweb.client.VHMainController;
import com.hockeo.vhweb.client.mvc.VHController;
import com.hockeo.vhweb.client.ui.base.VHTabItem;
import com.hockeo.vhweb.client.ui.widget.VHMatchdaySelector;
import com.hockeo.vhweb.client.ui.widget.VHRRMatchGrid;
import com.hockeo.vhweb.client.ui.widget.VHStandingsGrid;
import com.hockeo.vhweb.client.ui.widget.VHStandingsTypeSelector;
import com.hockeo.vhweb.client.utils.VHJsData;
import com.hockeo.vhweb.client.utils.enums.VHJsStandingsType;

/**
 * Displays the current standings of a given league. In addition, the matches of a
 * selected matchday are available. The standings can be switched between total, home and
 * away standings.
 *
 * @version $Id$
 * @author janjanke
 */
public class VHLeagueStandingsTab extends VHTabItem
{
  private final VHJsData          d_data;
  private final String            d_strElementId;
  private VHStandingsGrid         d_standingsGrid;
  private VHRRMatchGrid           d_matchGrid;
  private VHMatchdaySelector      d_matchdaySelector;
  private VHStandingsTypeSelector d_standingsTypeSelector;

  /**
   * Creates a new league standings tab.
   *
   * @param controller
   * @param leagueOrElement either a league or a round robin contest element
   */
  public VHLeagueStandingsTab( VHController controller, VHJsData leagueOrElement )
  {
    super( controller, VHMain.getConstants().standingsAndMatches() );

    d_data = leagueOrElement;

    if ( d_data.getType().equals( "GLeague" ) )
    {
      d_strElementId = d_data.getString( "regularSeasonElementId" );
      setIcon( VHMain.ICON_STANDINGS );
    }
    else
    {
      d_strElementId = leagueOrElement.getId();
      setText( VHMain.getMessages().roundRobinGroupHeader( leagueOrElement.getString( "label" ) ) );
    }
  }

  @Override
  public void buildContent()
  {
    BorderLayoutData data;
    final VHMainController ctr = (VHMainController) getController();

    if ( isContentReady() )
      return;

    ContentPanel panelNorth = new ContentPanel( new RowLayout( Style.Orientation.VERTICAL ) );
    panelNorth.setHeaderVisible( false );

    // matchday selector
    //
    d_matchdaySelector = new VHMatchdaySelector();
    ctr.loadMatchdays( d_matchdaySelector, d_strElementId );
    panelNorth.add( d_matchdaySelector, new RowData( Style.DEFAULT, Style.DEFAULT, new Margins( 4, 4, 4, 4 ) ) );

    // standings type selector
    //
    d_standingsTypeSelector = new VHStandingsTypeSelector();
    panelNorth.add( d_standingsTypeSelector, new RowData( Style.DEFAULT, Style.DEFAULT, new Margins( 0, 4, 4, 4 ) ) );

    data = new BorderLayoutData( LayoutRegion.NORTH );
    data.setMargins( new Margins( 4, 4, 4, 4 ) );
    data.setSize( 58f );
    add( panelNorth, data );

    // match list
    //
    d_matchGrid = new VHRRMatchGrid( ctr );
    ctr.loadMatches( d_matchGrid, d_strElementId, null );

    data = new BorderLayoutData( LayoutRegion.CENTER );
    data.setMargins( new Margins( 0, 4, 4, 4 ) );
    add( d_matchGrid, data );

    // standings table
    //
    d_standingsGrid = new VHStandingsGrid( getController(), VHStandingsGrid.DisplayType.STANDARD );
    ctr.loadStandings( d_standingsGrid, d_strElementId, VHJsStandingsType.TOTAL, null );

    data = new BorderLayoutData( LayoutRegion.SOUTH );
    data.setMargins( new Margins( 0, 4, 4, 4 ) );
    data.setSplit( true );
    data.setMinSize( VHStandingsGrid.HEIGHT_HEADER + VHStandingsGrid.HEIGHT_TEAM * 6 );
    data.setMaxSize( VHStandingsGrid.HEIGHT_HEADER + VHStandingsGrid.HEIGHT_TEAM * d_data.getInt( "numTeams" ) );
    data.setSize( VHStandingsGrid.HEIGHT_HEADER + VHStandingsGrid.HEIGHT_TEAM * d_data.getInt( "numTeams" ) );
    add( d_standingsGrid, data );

    // Add event listeners
    //
    d_matchdaySelector.getCombobox().addSelectionChangedListener( new SelectionChangedListener<VHJsData>() {
      @SuppressWarnings( "synthetic-access" )
      @Override
      public void selectionChanged( SelectionChangedEvent<VHJsData> se )
      {
        if ( !d_matchdaySelector.isInitialised() )
          return;

        VHJsData mdSelected = se.getSelectedItem();
        ctr.loadMatches( d_matchGrid, d_strElementId, mdSelected.getInt( "num" ) );
        ctr.loadStandings( d_standingsGrid, d_strElementId,
            VHJsStandingsType.valueOf( d_standingsTypeSelector.getCombobox().getValue().getId() ),
            mdSelected.getInt( "num" ) );
      }
    } );

    d_standingsTypeSelector.getCombobox().addSelectionChangedListener( new SelectionChangedListener<VHJsData>() {
      @SuppressWarnings( "synthetic-access" )
      @Override
      public void selectionChanged( SelectionChangedEvent<VHJsData> se )
      {
        if ( !d_matchdaySelector.isInitialised() )
          return;

        VHJsData st = se.getSelectedItem();
        ctr.loadStandings( d_standingsGrid, d_strElementId, VHJsStandingsType.valueOf( st.getId() ),
            d_matchdaySelector.getCombobox().getValue().getInt( "num" ) );
      }
    } );

    super.buildContent();
  }
}
